package com.coin900.coin.service.fu;

import com.coin900.coin.base.FuConstants;
import com.coin900.coin.base.utils.GoogleAuthenticator;
import com.coin900.coin.base.utils.SessionUtils;
import com.coin900.coin.persist.fu.FuUserAuthVO;
import com.coin900.coin.persist.fu.FuUserShareRecordVO;
import com.coin900.coin.persist.fu.FuUserVO;
import com.coin900.coin.service.coin.IFuUserShareRecordService;
import com.value.exception.BusinessException;
import com.value.mybatis.IDaoRouter;
import com.value.mybatis.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author shui
 * @create 2017-11-21
 **/
@Service
public class FuUserService implements IFuUserService {

    @Override
    public List<FuUserVO> loadFuUserForPage(Pagination page) {
        return this.daoRouter.query("FuUser.loadFuUserForPage", page);
    }

    @Override
    public List<FuUserVO> loadFuUser(Map<String, Object> params) {
        return this.daoRouter.query("FuUser.loadFuUser", params);
    }

    @Override
    public List<FuUserVO> loadProjectUsers(Map<String, Object> params) {
        return this.daoRouter.query("FuUser.loadProjectUsers", params);
    }

    @Override
    public FuUserVO getUser(Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("dataStatus", 1);
        params.put("userStatus", 1);
        List<FuUserVO> fuUserVOS = this.loadFuUser(params);
        return fuUserVOS.get(0);
    }

    @Override
    public void insertFuUser(FuUserVO userVO) {
        userVO.setUuid(UUID.randomUUID().toString());
        userVO.setUserStatus(1);
        userVO.setSafetyLevel(FuConstants.FU_SAFETY_LEVEL_LOW);
        userVO.setEmailStatus(0);
        userVO.setIdcardStatus(0);
        userVO.setMoblieStatus(0);
        userVO.setDataStatus(1);
        this.daoRouter.insert("FuUser.insertFuUser", userVO);
    }

    @Override
    public void insertFuUser(FuUserVO fuUserVO, Long shareUserId) {
        this.insertFuUser(fuUserVO);
        FuUserShareRecordVO fuUserShareRecordVO = new FuUserShareRecordVO();
        fuUserShareRecordVO.setShareUserId(shareUserId);
        fuUserShareRecordVO.setUserId(fuUserVO.getId());
        fuUserShareRecordVO.setCreatedBy(fuUserVO.getCreatedBy());
        fuUserShareRecordVO.setCreatedDate(fuUserVO.getCreatedDate());
        fuUserShareRecordVO.setDataStatus(1);
        this.shareRecordService.insertFuUserShareRecord(fuUserShareRecordVO);
    }

    @Override
    public void insertFuUserByBxLogin(FuUserVO fuUserVO) {
        // 新建 tokenok 用户
        fuUserVO.setUserType(FuConstants.FU_USER_TYPE_COIN900);
        fuUserVO.setUuid(UUID.randomUUID().toString());
        fuUserVO.setUserStatus(1);
        fuUserVO.setMoblieStatus(0);
        fuUserVO.setDataStatus(1);
        fuUserVO.setSafetyLevel(FuConstants.FU_SAFETY_LEVEL_LOW);
        this.daoRouter.insert("FuUser.insertFuUser", fuUserVO);
        Long userId = fuUserVO.getId();
        FuUserAuthVO userAuthVO = fuUserVO.getUserAuthVO();
        if (userAuthVO != null) {
            // 有认证信息则插入认证信息
            userAuthVO.setUserId(userId);
            userAuthVO.setDataStatus(1);
            userAuthVO.setCheckStatus(FuConstants.FU_CHECK_STATUS_SUCCESS);
            this.userAuthService.insertFuUserAuth(userAuthVO);
            // 修改用户的认证状态:已认证
            FuUserVO userVO = new FuUserVO();
            userVO.setId(userId);
            userVO.setIdcardStatus(1);
            this.updateFuUser(userVO);
        }
    }

    @Override
    public void updateFuUser(FuUserVO vo) {
        this.daoRouter.update("FuUser.updateFuUser", vo);
    }

    @Override
    public void deleteFuUser(FuUserVO vo) {
        daoRouter.update("FuUser.deleteFuUser", vo);
    }

    @Override
    public void doBindGoogleSecret(FuUserVO user, Long code, String googleSecret) {
        //验证
        long t = System.currentTimeMillis();
        GoogleAuthenticator ga = new GoogleAuthenticator();
        ga.setWindowSize(5);
        boolean check = ga.check_code(googleSecret, code, t);
        if(check){
            //验证成功绑定
            user.setGoogleStatus(1);
            user.setGoogleSecret(googleSecret);
            user.setModifiedInfor(user);
            daoRouter.update("FuUser.updateGoogleSecret", user);

            FuUserVO sessionUser = SessionUtils.getUser();
            sessionUser.setGoogleSecret(user.getGoogleSecret());
            sessionUser.setGoogleStatus(user.getGoogleStatus());
            SessionUtils.setUser(user);
        }else{
            throw new BusinessException("请输入正确的验证码");
        }
    }

    @Override
    public void doCancelGoogleSecret(FuUserVO user, Long code) {
        //验证
        long t = System.currentTimeMillis();
        GoogleAuthenticator ga = new GoogleAuthenticator();
        ga.setWindowSize(5);
        String googleSecret = user.getGoogleSecret();
        boolean check = ga.check_code(googleSecret, code, t);
        if(check){
            //验证成功取消绑定
            user.setGoogleStatus(0);
            user.setGoogleSecret(googleSecret);
            user.setModifiedInfor(user);
            daoRouter.update("FuUser.updateGoogleSecret", user);


            FuUserVO sessionUser = SessionUtils.getUser();
            sessionUser.setGoogleSecret(user.getGoogleSecret());
            sessionUser.setGoogleStatus(user.getGoogleStatus());
            SessionUtils.setUser(user);
        }else{
            throw new BusinessException("请输入正确的验证码");
        }
    }

    @Autowired
    private IFuUserShareRecordService shareRecordService;
    @Autowired
    private IFuUserAuthService userAuthService;
    @Autowired
    private IDaoRouter daoRouter;

}